package com.example.anyong17_julnumgi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class TimeSetActivity extends Activity {
        int maxrange = 60, minrange = 1, value = 0;
        int maxCounter =999, minCounter =1, counterValue= 0;
        
        int set_value=0;//save 후 공유되는값
        int set_counterValue=0;//save 후 공유되는값
        
        Boolean set_time_OnOff= false;//save 후 공유되는값
        Boolean set_count_OnOff= false;//save 후 공유되는값
        Boolean set_sound_OnOff=false;//save 후 공유되는값
        
        EditText numberEdit;
        EditText counterEdit;
        
        CheckBox check_time;
        CheckBox check_count;
        CheckBox check_sound;
        
        Boolean time_OnOff= false;
        Boolean count_OnOff= false;
        Boolean sound_OnOff=true;
        
        
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.timeset);
                        Button upButton, downButton;
                        Button counter_upButton, counter_downButton;
                
                        SharedPreferences set_pref = getSharedPreferences("SET", 0);//time설정
                        value=set_pref.getInt("time_value", 0);
                        counterValue=set_pref.getInt("count_value", 0);
                        time_OnOff=set_pref.getBoolean("time_OnOff", false);
                        count_OnOff=set_pref.getBoolean("count_OnOff", false);
                        sound_OnOff=set_pref.getBoolean("sound_OnOff", true);

                        set_time_OnOff=time_OnOff;
                        set_count_OnOff=count_OnOff;
                        set_sound_OnOff=sound_OnOff;
                        
                        set_value=value;
                        set_counterValue=counterValue;                  
                        
                         upButton = (Button) findViewById(R.id.timeset_upButton);
                         numberEdit = (EditText) findViewById(R.id.timeset_numberEditText);
                         downButton = (Button) findViewById(R.id.timeset_downButton);
                         numberEdit.setHint(""+set_value);
                
                         
                         counter_upButton= (Button) findViewById(R.id.timeset_counter_upButton);
                         counterEdit= (EditText) findViewById(R.id.timeset_counter_numberEditText);
                         counter_downButton = (Button) findViewById(R.id.timeset_counter_downButton);
                         counterEdit.setHint(""+set_counterValue);
                 
                         check_time=(CheckBox) findViewById(R.id.timeset_chk_01);       
                         check_time.setChecked(set_time_OnOff); 
                         
                         check_count=(CheckBox) findViewById(R.id.timeset_chk_02);
                         check_count.setChecked(set_count_OnOff);
                         
                         check_sound=(CheckBox) findViewById(R.id.timeset_option_chk_01);
                         check_sound.setChecked(set_sound_OnOff);
                         
                         
                         
                         
                         
                         numberEdit.addTextChangedListener(new TextWatcher() {
                         
                                         @Override
                                         public void beforeTextChanged(CharSequence s, int start, int count,
                                         int after) {
                                         // TODO Auto-generated method stub
                                
                                         }
                                         @Override
                                         public void afterTextChanged(Editable arg0) {
                                         // TODO Auto-generated method stub
                                         }
                                        @Override
                                        public void onTextChanged(CharSequence s, int start, int before,
                                                        int count) {
                                                // TODO Auto-generated method stub
                                                
                                                try{
                                                        value= Integer.parseInt(s.toString());
                                                }catch(NumberFormatException e){}
                                        }
                         });
                        counterEdit.addTextChangedListener(new TextWatcher() {
                         
                                         @Override
                                         public void beforeTextChanged(CharSequence s, int start, int count,
                                         int after) {
                                         // TODO Auto-generated method stub
                                
                                         }
                                         @Override
                                         public void afterTextChanged(Editable arg0) {
                                         // TODO Auto-generated method stub
                                         }
                                        @Override
                                        public void onTextChanged(CharSequence ss, int start, int before,
                                                        int count) {
                                                // TODO Auto-generated method stub
                                                
                                                try{
                                                        counterValue= Integer.parseInt(ss.toString());
                                                }catch(NumberFormatException e){}
                                        }
                         });
                         
                 
        }
        

        @Override
        protected void onPause() {
                super.onPause();
                SharedPreferences pref = getSharedPreferences("SET", 0);
                SharedPreferences.Editor edit = pref.edit();
                
                
                edit.putInt("time_value", set_value);
                edit.putInt("count_value", set_counterValue);
                edit.putBoolean("time_OnOff", set_time_OnOff);
                edit.putBoolean("count_OnOff", set_count_OnOff);
                edit.putBoolean("sound_OnOff", set_sound_OnOff);
                
        
                edit.commit();
        }
        
        
        public void mOnClick(View v) {
                switch (v.getId()) {
                        case R.id.timeset_upButton:
                                        if (value >= minrange && value <= maxrange)
                                        value++;
                                        if (value > maxrange)
                                        value = minrange;
                                        numberEdit .setText(value + "");
                                        //Toast.makeText(this, "maxrange: "+maxrange+"minrange: "+minrange+"value : "+value, Toast.LENGTH_LONG).show();
                                        break;
                        
                        case R.id.timeset_downButton:
                                        if (value >= minrange && value <= maxrange)
                                        value--;
                                        if (value < minrange)
                                        value = maxrange;
                                        numberEdit .setText(value + "");
                                        //Toast.makeText(this, "maxrange: "+maxrange+"minrange: "+minrange+"value : "+value, Toast.LENGTH_LONG).show();
                                        break;
                        case R.id.timeset_counter_upButton:
                                        if (counterValue >= minCounter && counterValue <= maxCounter)
                                        counterValue++;
                                        if (counterValue > maxCounter)
                                        counterValue = minCounter;
                                        counterEdit .setText(counterValue + "");
                                        //Toast.makeText(this, "maxCounter : "+maxCounter+"minCounter : "+minCounter+"counterValue : "+counterValue, Toast.LENGTH_LONG).show();
                                        break;
                        
                        case R.id.timeset_counter_downButton:
                                        if (counterValue >= minCounter && counterValue <= maxCounter)
                                        counterValue--;
                                        if (counterValue < minCounter)
                                        counterValue = maxCounter;
                                        counterEdit .setText(counterValue + "");
                                    //Toast.makeText(this, "maxCounter: "+maxCounter+"minCounter: "+minCounter+"counterValue : "+counterValue, Toast.LENGTH_LONG).show();
                                        break;
                        case R.id.timeset_save_btn:
                                        
                                        set_counterValue=counterValue;
                                        set_value=value;
                                        set_count_OnOff=count_OnOff;
                                        set_time_OnOff=time_OnOff;
                                        set_sound_OnOff=sound_OnOff;
                                        /*if(){
                                                Toast.makeText(this, "maxCounter: "+maxCounter+"minCounter: "+minCounter+"counterValue : "+counterValue, Toast.LENGTH_LONG).show();
                                        }else{*/
                                                //Toast.makeText(this, "줄넘기seting 값: "+counterValue+"\n"+"시간 setting 값: "+value+"\n"+"줄넘기_OnOff : "+set_count_OnOff+"\n"+"시간_OnOff : "+set_time_OnOff+"\n"+"저장되었습니다.", Toast.LENGTH_LONG).show();
                                        Toast.makeText(this, "저장되었습니다.", Toast.LENGTH_LONG).show();
                                        //}
                                        break;
                        case R.id.timeset_chk_01://time check 실행시
                                        if(check_count.isChecked())check_count.setChecked(false);
                                                if(check_time.isChecked()){
                                                        time_OnOff=true;
                                                        count_OnOff=false;
                                                }else{
                                                        time_OnOff=false;
                                                }
                                                //Toast.makeText(this, "time_OnOff : "+time_OnOff+"count_OnOff : "+count_OnOff, Toast.LENGTH_LONG).show();
                                        break;
                        case R.id.timeset_chk_02://count 체크
                                        if(check_time.isChecked())check_time.setChecked(false);
                                                if(check_count.isChecked()){
                                                        count_OnOff=true;
                                                        time_OnOff=false;
                                                }else{
                                                        count_OnOff=false;
                                                }
                                                //Toast.makeText(this, "time_OnOff : "+time_OnOff+"count_OnOff : "+count_OnOff, Toast.LENGTH_LONG).show();
                                        break;
                        case R.id.timeset_option_chk_01 :
                        				if(check_sound.isChecked()){
                        					sound_OnOff=true;
                        				}else{
                        					sound_OnOff=false;
                        				}
                        	
                        				break;
                }
                
        }
        @Override
        protected void onDestroy() {
        	 RecycleUtils.recursiveRecycle(getWindow().getDecorView());
             System.gc();
             super.onDestroy();
        }
}